<?php

namespace App\Models\Game\Config\Game;

use App\Models\Game\Platform\Org;
use App\Models\Traits\Game\LogsActivity;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Platform extends System
{
    use LogsActivity;

    /**
     * 数据表名称
     *
     * @var string
     */
//    protected $table = 't_config_game_plat_copy1';
    protected $table = 't_config_game_plat';

    /**
     * 获得平台信息
     *
     * @return BelongsTo
     */
    public function org(): BelongsTo
    {
        return $this->belongsTo(Org::class, 'plat_code', 'code')->withTrashed();
    }

    /**
     * 平台配置结构体
     *
     * @param string $gameCode
     * @return array
     */
    protected function getConfigStruct($gameCode)
    {
        // slo【基本】
        if (in_array($gameCode, [
            'slobskg', 'slobloodsucker', 'slofruitrush', 'slosuper777',
            'slocandy', 'slopirate', 'slomermaid', 'slokingofspeed',
            'slopharaohcoins', 'slosamuraipreach', 'slothechickenfarm',
            'slospacetravel', 'slobxxb', 'sloadventureisland', 'slothebookofcarl',
            'slomoneytrain', 'slowitchelf', 'slotherichman', 'slothemagicians',
            'slowrathofolympus', 'slogoddess', 'sloworldcupchampions',
            'slowanteddeadorawild', 'slopokerlife', 'slocockfighting',
            'slomahjong', 'sloanimalparty', 'slomyantemple', 'slodragonfortune',
            'sloella', 'sloiceage', 'slodragonquest', 'slowestworld', 'sloaladdin',
            'sloraveclub', 'slofacaishen', 'slodog', 'slosweetmelee', 'slozeus',
            'sloroulette', 'slocaptainpenguin', 'sloluckyjoker',
        ])) {
            return [
                'scenes' => [
                    1001 => ['control' => ['model_id' => null]]
                ]
            ];
        }
        // slo【初级】
        if (in_array($gameCode, ['slostarburst', 'slohockey'])) {
            return [
                'scenes' => [
                    1001 => ['control' => ['model_id' => null]]
                ],
                'globals' => [
                    'wild_create_per_col' => null
                ]
            ];
        }
        // fish
        if (in_array($gameCode, ['fishjackpot'])) {
            return [
                'globals' => [
                    'global_revise' => null
                ]
            ];
        }

        // tiger
        if (in_array($gameCode, ['sloparrot' , 'slocaptainsboundy' , 'slogenies3wishes' , 'slossff' , 'slosbjn'])) {
            return [
                'scenes' => [
                    1001 => ['control' => ['model_id' => null]]
                ],
            ];
        }
        return [];
    }

    /**
     * 查询一条数据
     *
     * @param string $platCode
     * @param string $gameCode
     * @return \Illuminate\Database\Eloquent\Model
     */
    protected function findByPlatGame($platCode, $gameCode)
    {
        return $this->newQuery()
            ->where('plat_code', $platCode)
            ->where('game_id', $gameCode)
            ->first();
    }

    /**
     * 更新或创建一条数据
     *
     * @param string $platCode
     * @param string $gameCode
     * @param array $config
     * @return \Illuminate\Database\Eloquent\Model
     */
    protected function updateOrCreateByPlatGame($platCode, $gameCode, array $config = [])
    {
        if (empty($config['globals'])) {
            $config['globals'] = new \stdClass();
        }

        return $this->newQuery()->updateOrCreate([
            'plat_code' => $platCode,
            'game_id' => $gameCode
        ], [
            'config' => $config,
        ]);
    }
}
